// pages/staff/index.js
var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    logo: '../../static/images/logo.png',
    shopname: '',
    name: '',
    username: '',
    openid:'',
    userid:'',
    right1:0,
    right2:0,
    yGiveNumber: 0,
    yGiveAmount: '0.00',
    tGiveNumber: 0,
    tGiveAmount: '0.00',
    yCheckNumber: 0,
    yCheckAmount: '0.00',
    tCheckNumber: 0,
    tCheckAmount: '0.00',
    yInputNumber: 0,
    yInputAmount: '0.00',
    tInputNumber: 0,
    tInputAmount: '0.00',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('onload');
    app.util.checkLogin();
    this.setInfo();
    this.couponData();
  },

  onShow: function (options) {
    console.log('onshow');
    app.util.checkLogin();
    this.setInfo();
  },

  setInfo: function() {
    const user = wx.getStorageSync('user');
    const openid = wx.getStorageSync('openid');
    if(openid){
      this.setData({
        openid:openid
      })
    }else{
      this.setData({
        openid: user.openid,
      })
    }
    this.setData({
      shopname: user.shopname,
      logo: user.logo,
      name: user.name,
      username: user.username,
      userid: user.id,
      right1: user.right1,
      right2: user.right2
    });
  },

  couponData: function(){
    var that=this;
    wx.login({
      success: function (res) {
        if (res.code) {
          //发起网络请求
          app.util.request({
            url: 'entry/wxapp/couponData2',
            data: {
              userid: that.data.userid
            },
            cachetime: 0,
            success: function (res) {
              var res = res.data.data;
              that.setData({
                yGiveNumber: res.yGiveNumber,
                yGiveAmount: res.yGiveAmount,
                yCheckNumber: res.yCheckNumber,
                yCheckAmount: res.yCheckAmount,
                yInputNumber: res.yInputNumber,
                yInputAmount: res.yInputAmount,
                tGiveNumber: res.tGiveNumber,
                tGiveAmount: res.tGiveAmount,
                tCheckNumber: res.tCheckNumber,
                tCheckAmount: res.tCheckAmount,
                tInputNumber: res.tInputNumber,
                tInputAmount: res.tInputAmount,
              })
              //隐藏loading 提示框
              wx.hideLoading();
              //隐藏导航条加载动画
              wx.hideNavigationBarLoading();
              //停止下拉刷新
              wx.stopPullDownRefresh();
            }
          })
        }
      }
    });
  },
  getopenid: function(){
    var that=this;
    wx.login({
      success: function (res) {
        if (res.code) {
          //发起网络请求
          app.util.request({
            url: 'entry/wxapp/getOpenid',
            data: {
              code: res.code,
              userid: that.data.userid
            },
            cachetime: 0,
            success: function (res) {
              var openid = res.data.data.openid;
              if(openid){
                wx.showToast({
                  icon:'none',
                  title: '绑定成功',
                  duration: 2000
                });	
                wx.setStorageSync('openid', openid);
                that.setData({
                  openid: openid
                })
              }
            }
          })
        }
      }
    });
  },

  changPwd: function(){
    wx.navigateTo({
      url: 'password',
    })
  },

  logout: function(){
    wx.showModal({
      title: "退出提示",
      content: "",
      showCancel: true,
      cancelText: "点错了",
      cancelColor: "#999999",
      confirmText: "退出登录",
      confirmColor: "#3CC51F",
      success: res => {
        if (res.confirm) {
          this.setData({
            logo: '../../static/images/logo.png',
            shopname: '',
            name: '',
            username: '',
            openid:'',
            userid:'',
            giveNumber: 0,
            giveAmount: '0.00',
            checkNumber: 0,
            checkAmount: '0.00',
            inputNumber: 0,
            inputAmount: '0.00'
          });
          wx.clearStorageSync();
          wx.showToast({
            icon:'success',
            title: '退出成功',
            duration: 2000
          });
          setTimeout(function() {
            wx.reLaunch({
              url: "/pages/staff/index"
            });
          }, 1500);
        }
      }
    });
  },
  verify: function () {
    var that = this;
    wx.scanCode({ 
      onlyFromCamera: true,
      scanType: ['qrCode'],
      success(res) {
        wx.navigateTo({
          url: 'check?sncode=' + res.result,
        })
      }
    })
 },
  setamount: function(){
    wx.navigateTo({
      url: '/pages/staff/setamount',
    })
  },

  sncode: function(){
    wx.navigateTo({
      url: '/pages/staff/sncode',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.couponData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})